<?php
include_once('common.inc.php');
class Wordpress {
	var  $siteurl = 'http://127.0.0.1';
	function __construct() {
		global $db;
	}
	//	批量转换
	private function name_slug(){
		global $db;
		$sql = "SELECT * FROM `wp_terms`";
		$rs = $db->query($sql);
		while ($rw = $db->fetch_array($rs)) {
			$name = urlencode($rw['name']);
			$sql2 = "update `wp_terms` set `slug` = '".$name."' where `term_id` = ".$rw['term_id'];
			$rs2 = $db->query($sql2);
		}
	}
	function add_cat($name){
		global $db;
		$re =array();
		if (is_array($name)) {
			$i = 0;
			foreach ($name as $v) {
				if (!$this->is_here($v)) {
					$sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$v}', '".urlencode($v)."', 0);";
					$rs = $db->query($sql);
					$num = $db->insert_id();
					$re[$i] = $num;
					$sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
		(NULL, {$num}, 'category', '', 0, 0)";
					$rs = $db->query($sql1);
					$i++;
				}
			}
			return $re;
		}else {
			if (!$this->is_here($name)) {
				$sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$name}', '".urlencode($name)."', 0);";
				$rs = $db->query($sql);
				$num = $db->insert_id();
				$sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
		(NULL, {$num}, 'category', '', 0, 0)";
				$rs = $db->query($sql1);
				return $num;
			}
		}
		return $num;
	}
	function add_tags($name){
		global $db;
		$re =array();
		if (is_array($name)) {
			$i = 0;
			foreach ($name as $v) {
				if (!$this->is_here($v,'post_tag')) {
					$sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$v}', '".urlencode($v)."', 0);";
					$rs = $db->query($sql);
					$num = $db->insert_id();
					$re[$i] = $num;
					$sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
		(NULL, {$num}, 'post_tag', '', 0, 0)";
					$rs = $db->query($sql1);
					$i++;
				}
			}
			return $re;
		}else {

			if (!$this->is_here($name,'post_tag')) {
				$sql = "INSERT INTO `wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, '{$name}', '".urlencode($name)."', 0);";
				$rs = $db->query($sql);
				$num = $db->insert_id();
				$sql1 = "INSERT INTO `wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES
		(NULL, {$num}, 'post_tag', '', 0, 0)";
				$rs = $db->query($sql1);
				return $num;
			}
		}
	}
	//post_tag
	function is_here($name,$type = 'category'){
		global $db;
		$sql ="SELECT s.`term_id`  FROM `wp_terms` s, `wp_term_taxonomy` y
		WHERE s.`name` = '{$name}'
		AND y.`taxonomy` = '{$type}'
		AND s.`term_id` = y.`term_id`";
		$rs = $db->query($sql);
		if ($num = $db->affected_rows()) {
			return true;
		}else {
			return false;
		}
	}
	function add_post($row){
		global $db;
		$time = date("Y-m-d H:i:s",time());
		$sql = "INSERT INTO `wp_posts`
		(`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_category`, `post_excerpt`, `post_status`, `comment_status`,
		`ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`,
		`menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES
		(NULL, 1, '{$time}', '{$time}',
		'{$row['content']}', '{$row['title']}', 0, '', 'publish', 'open', 'open', '',
		'".urlencode($row['title'])."', '', '', '{$time}', '{$time}',
		'', 0, '{$siteurl}/wp/?p=3', 0, 'post', '', 0)";
		$rs = $db->query($sql);
		if ($rs) {
			$id = $db->insert_id();
			$sql_u = "UPDATE `wp_posts` SET `guid` = '".$siteurl."/wp/?p=".$id."' WHERE `wp_posts`.`ID` ={$id} LIMIT 1 ;";
			$db->query($sql_u);
			//建立分类关联
			$cate = $this->add_cat($row['category']);
			if (!empty($cate)) {
				$sql = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ";
				if (is_array($cate)) {
					foreach ($cate as $v) {
						$sql .="({$id}, $v),";
					}
				}else {
					$sql .= "({$id}, $cate),";
				}
			}

			//建立标签关联
			$tags = $this->add_tags($row['tags']);
			if (!empty($tags)) {
				if (is_array($tags)) {
					foreach ($tags as $s) {
						$sql .="({$id}, $s),";
					}
				}else {
					$sql .= "({$id}, $tags),";
				}
				$sql = substr($sql,0,-1).";";
				$rs = $db->query($sql);
			}

		}

	}
}
$wp = new	Wordpress();
$rw['title'] = 'title';
$rw['content'] = 'content';
$rw['tags'] = array('黑色','美丽');
$rw['category'] = array('黑色1','美丽2');
$wp->add_post($rw);
//$wp->name_slug();